﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;

namespace Sunboxsoft.ASMS.WebUtility
{
    public class CookieUtility
    {
        private static string UserNameKey = "asms-user";
        private static string UserIdKey = "asms-userid";
        private static string BusinessIdKey = "asms-businessid";
        private static string BusinessNameKey = "asms-business";
        private static string StationIdKey = "asms-stationid";
        public static void SetUserId(Guid userId, string userName)
        {
            HttpCookie cookie = new HttpCookie(UserIdKey, userId.ToString());
            cookie.Expires = DateTime.Now.AddDays(2);
            HttpContext.Current.Response.Cookies.Add(cookie);

            HttpCookie cookieName = new HttpCookie(UserNameKey, HttpUtility.UrlEncode(userName));
            cookieName.Expires = DateTime.Now.AddDays(2);
            HttpContext.Current.Response.Cookies.Add(cookieName);
        }

        public static void SetBusinessId(Guid? businessId, string businessName)
        {
            if (businessId == null)
            {
                HttpCookie cookie = new HttpCookie(BusinessIdKey, string.Empty);
                cookie.Expires = DateTime.Now.AddDays(2);
                HttpContext.Current.Response.Cookies.Add(cookie);
            }
            else
            {
                HttpCookie cookie = new HttpCookie(BusinessIdKey, businessId.Value.ToString());
                cookie.Expires = DateTime.Now.AddDays(2);
                HttpContext.Current.Response.Cookies.Add(cookie);
            }

            if (string.IsNullOrEmpty(businessName))
            {
                HttpCookie cookie = new HttpCookie(BusinessNameKey, string.Empty);
                cookie.Expires = DateTime.Now.AddDays(2);
                HttpContext.Current.Response.Cookies.Add(cookie);
            }
            else
            {
                HttpCookie cookie = new HttpCookie(BusinessNameKey, HttpUtility.UrlEncode(businessName));
                cookie.Expires = DateTime.Now.AddDays(2);
                HttpContext.Current.Response.Cookies.Add(cookie);
            }
        }

        public static void SetStationId(Guid? stationId)
        {
            if (stationId == null)
            {
                HttpCookie cookie = new HttpCookie(StationIdKey, string.Empty);
                cookie.Expires = DateTime.Now.AddDays(2);
                HttpContext.Current.Response.Cookies.Add(cookie);
            }
            else
            {
                HttpCookie cookie = new HttpCookie(StationIdKey, stationId.Value.ToString());
                cookie.Expires = DateTime.Now.AddDays(2);
                HttpContext.Current.Response.Cookies.Add(cookie);
            }
        }

        public static void RemoveUser()
        {
            HttpCookie cookieId = new HttpCookie(UserIdKey, string.Empty);
            cookieId.Expires = DateTime.Now.AddDays(-2);
            HttpContext.Current.Response.Cookies.Add(cookieId);

            HttpCookie cookieName = new HttpCookie(UserNameKey, string.Empty);
            cookieName.Expires = DateTime.Now.AddDays(-2);
            HttpContext.Current.Response.Cookies.Add(cookieName);
        }
        public static void RemoveBusinessId()
        {
            HttpCookie cookie = new HttpCookie(BusinessIdKey, string.Empty);
            cookie.Expires = DateTime.Now.AddDays(-2);
            HttpContext.Current.Response.Cookies.Add(cookie);
            HttpCookie cookieName = new HttpCookie(BusinessNameKey, string.Empty);
            cookieName.Expires = DateTime.Now.AddDays(-2);
            HttpContext.Current.Response.Cookies.Add(cookieName);
        }
        public static void RemoveStationId()
        {
            HttpCookie cookie = new HttpCookie(StationIdKey, string.Empty);
            cookie.Expires = DateTime.Now.AddDays(-2);
            HttpContext.Current.Response.Cookies.Add(cookie);
        }

        public static Guid? GetUserId()
        {
            HttpCookie cookie = HttpContext.Current.Request.Cookies.Get(UserIdKey);
            if (cookie == null)
            {
                return null;
            }
            Guid userId = Guid.Empty;
            if (Guid.TryParse(cookie.Value, out userId))
            {
                return userId;
            }
            return null;
        }
        public static string GetUserName()
        {
            HttpCookie cookie = HttpContext.Current.Request.Cookies.Get(UserNameKey);
            if (cookie == null)
            {
                return null;
            }
            return HttpUtility.UrlDecode(cookie.Value);
        }

        public static Guid? GetBusinessId()
        {
            HttpCookie cookie = HttpContext.Current.Request.Cookies.Get(BusinessIdKey);
            if (cookie == null)
            {
                return null;
            }
            Guid id = Guid.Empty;
            if (Guid.TryParse(cookie.Value, out id))
            {
                return id;
            }
            return null;
        }

        public static string GetBusinessName()
        {
            HttpCookie cookie = HttpContext.Current.Request.Cookies.Get(BusinessNameKey);
            if (cookie == null)
            {
                return null;
            }
            return HttpUtility.UrlDecode(cookie.Value);
        }

        public static Guid? GetStationId()
        {
            HttpCookie cookie = HttpContext.Current.Request.Cookies.Get(StationIdKey);
            if (cookie == null)
            {
                return null;
            }
            Guid id = Guid.Empty;
            if (Guid.TryParse(cookie.Value, out id))
            {
                return id;
            }
            return null;
        }

        public static string GetUserIdString()
        {
            Guid? id = GetUserId();
            if (id == null)
            {
                return Guid.Empty.ToString();
            }
            return id.ToString();
        }
    }
}
